using System;

using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.HighLevel.GameEngine2D;
using Sce.PlayStation.HighLevel.GameEngine2D.Base;

namespace UWSGameJame
{
	public class BalloonGenerator
	{
		private Vector2 position;
		private Balloon currentBalloon;
		private const float fillRate = 0.05f;
		private bool released = false;
		private SpriteUV pipeSystem;
		private Texture2D pipeTex;
		private Vector2 originalPosition;
		
		public BalloonGenerator ()
		{
			position = new Vector2(0, 0);
			currentBalloon = new Balloon("Application/Resources/Textures/red_balloon.png", 10.0f);
		}
		
		public BalloonGenerator(GameScene s, Vector2 position)
		{
			this.position = position;
			originalPosition = position;
			
			pipeTex = new Texture2D("Application/Resources/Textures/pipeSystem.png", false);
			TextureInfo ti = new TextureInfo(pipeTex);
			pipeSystem = new SpriteUV();
			
   			pipeSystem.TextureInfo = ti;
   
			pipeSystem.Quad.S = ti.TextureSizef;
			pipeSystem.CenterSprite ();	
			
			pipeSystem.Position = new Vector2((this.position.X - ti.TextureSizef.X / 2) + 15.0f, this.position.Y + 15.0f);
			s.AddChild(pipeSystem);
			
			currentBalloon = new Balloon("Application/Resources/Textures/red_balloon.png", 10.0f);
			currentBalloon.setPosition(position);
			currentBalloon.makeStatic();
			currentBalloon.addToScene(s);
			
			s.getPhysics().addBody(currentBalloon.getPhysicsShape(), currentBalloon.getPhysicsBody());
		}
		
		public void release(BalloonScene phys)
		{
			currentBalloon.makeDynamic();
			released = true;
		}
			
		public void update(GameScene s, BalloonScene phys)
		{
			if(!released)
			{
				fill();
				
				if(currentBalloon.isPopped() && currentBalloon.endSplash(phys))
				{
					released = true;
				}
			}
			else
			{				
				if(currentBalloon.isPopped() && currentBalloon.endSplash(phys))
				{
					currentBalloon.setPosition(originalPosition);
					currentBalloon.makeStatic();
					currentBalloon.getSprite().Color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
					released = false;
				}
				else if(!currentBalloon.isPopped())
				{
					currentBalloon.activeUpdate();
				}
			}
		}
		
		public void fill()
		{
			currentBalloon.scale(fillRate, fillRate);
			currentBalloon.fill(fillRate);
		}
		
		public Balloon getBalloon()
		{	
			return this.currentBalloon;
		}
		
		public void fire()
		{
			currentBalloon.getPhysicsBody().velocity = currentBalloon.getPhysicsBody().mass * new Vector2(20.0f, 20.0f);
		}
	}
}

